import { intersect, planarArea, highlightByGeometryList, getBaseData } from "lark-map";
import QueryUtils from "@/utils/yc/QueryUtils";
import GeometryUtils from "@/utils/yc/GeometryUtils";
import Utils from "@/utils/yc/Utils";
import SymbolUtils from "@/utils/yc/SymbolUtils";
import { format } from '@/utils/yc/format'
/**
 * 生态划片指数
 */
export default {
  data() {
    return {

      /**
       * 查询数据配置
       */
      queryConfig: [
        {
          type: 'GridQueryService', name: '气象数据'
        },
        {
          type: 'GetFeaturesByGeometryService',
          name: '土壤数据'
        },
        {
          type: 'GetFeaturesByGeometryService', name: '植被数据'
        },
        {
          type: 'GetFeaturesByGeometryService', name: '修复技术'
        }
      ],
      //返回结果集合
      resultObject: {
        //类型
        type: '2',
        //基础数据
        baseData: {
          jd: 0, //经度
          wd: 0,//纬度
          xzqmc: "",//行政区名称
          mj: 0,//面积
          bh: "",//编号
        },
        //气象特征
        qxtz: {
          ljjy: "350ml~400ml",
          pjwd: "2°C~6°C",
          pjsd: "2°C~6°C",
          yxjw: "2°C~6°C",
          pjrzss: "124天26小时~128天17小时",
          dfts: "22天~28天",
          ghts: "22天~28天"
        },
        //地理特征
        dltz: {
          pd: "45°C~60°C",
          px: "0~360",
          hbgd: "1100m~1500m",
          styy: "0~10",
        },
        //土壤特征
        trtz: {
          trlx: "黑钙土",
          trsd: "20%~30%"
        },
        //集合图形
        features: []
      },
    }
  },
  methods: {

    /**
      * 点查询数据
      * @param {*} view    地图
      * @param {*} feature 行政区图形
      * @param {*} point   点图形
      */
    async queryDataBySlicing(view, feature, point) {
      view.graphics.removeAll();
      let geometry = intersect({ geometry: feature.geometry, intersector: point });
      let owner = this;
      if (geometry) {
        //查询数据
        getBaseData({ geometry: geometry, queryConfig: this.queryConfig })
          .then(async res => {
            let result = await format(res, view, geometry);
            if (result) {
              //地理特征
              owner.resultObject.qxtz = result.qxtz;
              //气象特征
              owner.resultObject.dltz = result.dltz;
              //土壤特征
              owner.resultObject.trtz = result.trtz;
            }
          });
        let res = await QueryUtils.queryByGeometry("pd_point", view, geometry);
        if (res && res.length > 0) {
          let features = res[0]["results"];
          let graphic = features[0]["feature"];
          let xzqRes = await QueryUtils.queryByGeometry("xzq_point", view, graphic.geometry);
          let name = "";
          if (xzqRes && xzqRes.length > 0) {
            let features = xzqRes[0]["results"];
            features.forEach(xzqFeature => {
              name += xzqFeature["feature"]["attributes"]["NAME"] + ",";
            });
            name = name.substring(0, name.length - 1);
          }

          let attributes = graphic.attributes;
          let mj = 0;
          if(attributes.hasOwnProperty("MJ")){
            mj = Number(attributes.MJ).toFixed(2);
          }
          let center = graphic.geometry.extent.center;
          //中心点渲染
          let symbol = SymbolUtils.setRenderer(graphic.attributes.PDJB);
          highlightByGeometryList({ view, geometryList: [graphic.geometry, center], symbol: [symbol, SymbolUtils.LOCATION_PICTURE] });
          let code = feature.attributes.CODE;
          let objectId = attributes.OBJECTID
          let fileName = Utils.getFileName(code, objectId);
          let geoJson = GeometryUtils.graphicToGeoJson([graphic]);
          //设置基础数据
          let x = center.longitude.toFixed(4);
          let y = center.latitude.toFixed(4);
          this.resultObject.baseData.jd = x === undefined ? 0 : x;
          this.resultObject.baseData.wd = y === undefined ? 0 : y;
          this.resultObject.baseData.xzqmc = feature.attributes.NAME + name;
          this.resultObject.baseData.mj = mj;
          this.resultObject.baseData.bh = fileName;
          //设置图形数据
          let sfeature = {
            fileName: fileName,
            jsonFeature: JSON.stringify(geoJson)
          }
          this.resultObject.features = [sfeature];
          return this.resultObject
        }
      }
    },
  }
}
